/*
 * Pentaho Data Integration
 *
 * Copyright (C) 2002-2017 by Hitachi Vantara : http://www.pentaho.com
 *
 * **************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */

package org.pentaho.di.ui.util;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;
import org.pentaho.di.core.plugins.PluginInterface;
import org.pentaho.di.core.plugins.StepPluginType;
import org.pentaho.di.i18n.BaseMessages;
import org.pentaho.di.ui.core.dialog.ShowHelpDialog;
import org.pentaho.di.ui.core.gui.GUIResource;
import org.yzbdl.lanius.core.database.dialog.DataOverrideHandler;

public class HelpUtils {

    private static final Class<?> PKG = HelpUtils.class;

    public static Button createHelpButton(final Composite parent, final String title, final PluginInterface plugin) {
        Button button = newButton(parent);
        button.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent arg0) {
                openHelpDialog(parent.getShell(), plugin);
            }
        });
        return button;
    }

    public static Button createHelpButton(final Composite parent, final String title, final String url,
        final String header) {
        Button button = newButton(parent);
        button.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent arg0) {
                openHelpDialog(parent.getShell(), title, url, header);
            }
        });
        return button;
    }

    private static Button newButton(final Composite parent) {
        Button button = new Button(parent, SWT.PUSH);
        button.setImage(GUIResource.getInstance().getImageHelpWeb());
        button.setText(BaseMessages.getString(PKG, "System.Button.Help"));
        button.setToolTipText(BaseMessages.getString(PKG, "System.Tooltip.Help"));
        FormData fdButton = new FormData();
        fdButton.left = new FormAttachment(0, 0);
        fdButton.bottom = new FormAttachment(100, 0);
        button.setLayoutData(fdButton);
        return button;
    }

    public static String getHelpDialogTitle(PluginInterface plugin) {
        if (plugin == null) {
            return "";
        }
        String msgKey = "";
        // TODO currently support only Step and JobEntry - extend if required.
        if (plugin.getPluginType().equals(StepPluginType.class)) {
            msgKey = "System.ShowHelpDialog.StepPluginType.Title";
        } else {
            msgKey = "System.ShowHelpDialog.JobEntryPluginType.Title";
        }
        return BaseMessages.getString(PKG, msgKey, plugin.getName());
    }

    public static ShowHelpDialog openHelpDialog(Shell shell, PluginInterface plugin) {
        if (shell == null || plugin == null) {
            return null;
        }
        // ldg test start
        try {
            // !-- 接口暂时留着
            // String url = NetUrl.HELP;
            // Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + (url + "/details?id="+plugin.getId())
            // );
            // 获取简写的插件分类
            String simplePluginName =
                plugin.getPluginType().getName().substring(plugin.getPluginType().getName().lastIndexOf(".") + 1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static ShowHelpDialog openHelpDialog(Shell shell, String dialogTitle, String url, String header) {
        // ldg test start
        try {
            // !-- 接口暂时留着
            // String helpUrl = NetUrl.HELP;
            // Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + helpUrl);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 数据库连接页面 打开驱动列表页面
     *
     * @param shell
     * @param dialogTitle
     * @param url
     * @param header
     * @param hadnler
     * @return
     * @author LC
     */
    public static ShowHelpDialog openHelpDialog(Shell shell, String dialogTitle, String url, String header,
        DataOverrideHandler hadnler) {
        ShowHelpDialog helpDlg = new ShowHelpDialog(shell, dialogTitle, url, header, hadnler);
        helpDlg.open();
        return helpDlg;
    }
}
